package cn.fintecher.pangolin.service.dataimp.request;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.enums.ImportTemplateType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 查询数据导入参数
 * @Date: 16:47 2019/8/26
 */

@Data
public class FindDataImportRequest {
    @ApiModelProperty(value = "委托方ID")
    private String principalId;

    @ApiModelProperty(value = "批次号")
    private String batchNumber;

    @ApiModelProperty(value = "数据导入状态")
    private ImportDataExcelStatus status;

    @ApiModelProperty(value = "导入类型")
    @NotNull(message = "import.type.is.required")
    private ImportTemplateType type;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (Objects.nonNull(this.type)) {
            builder.must(QueryBuilders.matchPhraseQuery("type.keyword", this.type.name()));
        } else {
            builder.mustNot(QueryBuilders.existsQuery("type.keyword"));
        }
        if (Objects.nonNull(this.principalId)) {
            builder.must(QueryBuilders.matchPhraseQuery("principalId.keyword", this.principalId));
        }
        if (StringUtils.isNotBlank(this.batchNumber)) {
            builder.must(QueryBuilders.matchPhraseQuery("batchNumber.keyword", this.batchNumber));
        }
        if (Objects.nonNull(this.status)) {
            builder.must(QueryBuilders.matchPhraseQuery("status.keyword", this.status.name()));
        }
        return builder;
    }
}
